<template>
  <div class="accountList">
    <div class="searchBox">
      <el-form :model="searchForm" ref="searchFormRef">
        <el-row>
          <el-form-item prop="orderSn">
            <div class="item">
              <span v-html="'订单编号'" class="nameSpan"></span>
              <el-input
                size="mini"
                v-model="searchForm.orderSn"
                placeholder="请输入订单编号"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <div class="item">
              <span v-html="'时间查询'" class="nameSpan"></span>
              <el-date-picker
                size="mini"
                class="dataPicker"
                v-model="searchForm.startTime"
                type="datetime"
                placeholder="开始时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="pickerOptions0"
                default-time="08:00:00"
              >
              </el-date-picker>
              <span style="color: #d9d9d9">-</span>

              <el-date-picker
                size="mini"
                class="dataPicker"
                v-model="searchForm.endTime"
                type="datetime"
                placeholder="截止时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="pickerOptions1"
                default-time="08:00:00"
              >
              </el-date-picker>
            </div>
          </el-form-item>
          <el-form-item prop="payChannelList">
            <div class="item">
              <span v-html="'支付方式'" class="nameSpan"></span>
              <el-select filterable 
                multiple 
                clearable
                v-model="searchForm.payChannelList"
                placeholder="请选择支付方式"
                size="mini"
              >
                <el-option
                  v-for="item in payOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="searchClick"
              icon="el-icon-search"
              type="primary"
              size="mini"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetClick"
              >重置</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div>
      <el-button
      style=" float: right;margin:10px"
        type="primary"
        size="mini"
        v-has="'financeMenu:pay:listExport'"
        @click="paydownloadClick()"
        >导出</el-button
      >
    </div>
    <div class="tableBox" ref="tableBoxRef">
      <el-table
       :height="605"
        v-loading="tableLoading"
        :header-cell-style="{
          background: '#F8F8F8',
          color: '#333',
          fontSize: '16px',
          fontWeight: '700',
        }"
        border
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column prop="flowSn" align="center" label="流水编号">
        </el-table-column>
        <el-table-column prop="orderSn" align="center" label="订单编号">
        </el-table-column>
        <el-table-column  align="center" label="客户信息"  width="300">
          <template slot-scope="scope">
            <div>{{ scope.row.companyName }}</div>
            <div>{{ scope.row.userName }}</div>
            <div>{{ scope.row.userPhone }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="payChannel" align="center" label="支付方式">
          <template slot-scope="scope">
            {{ payFormatter[scope.row.payChannel] }}
          </template>
        </el-table-column>
        <el-table-column prop="totalFee" align="center" label="金额">
        </el-table-column>
        <el-table-column prop="paySn" align="center" label="商户交易号">
        </el-table-column>

        <el-table-column prop="payTime" align="center" label="支付时间">
        </el-table-column>
      </el-table>

      <el-card class="footer" v-if="total > 0">
        <div>
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[15, 30, 50, 100]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      payOptions: [
        { label: "微信小程序", value: 1 },
        { label: "微信", value: 2 },
        { label: "支付宝", value: 3 },
        { label: "对公打款", value: 4 },
      ],
      payFormatter: {
        1: "微信小程序",
        2: "微信",
        3: "支付宝",
        4: "对公打款",
      },
      pickerOptions0: {
        disabledDate: (time) => {
          //如果选择了结束时间返回的时间就要不能大于开始时间并且当前之前的时间也不可以选择
          if (this.searchForm.endTime) {
            return (
              time.getTime() > new Date(this.searchForm.endTime).getTime() ||
              time.getTime() < Date.now() - 8.64e7
            );
          }
        },
      },
      pickerOptions1: {
        disabledDate: (time) => {
          if (this.searchForm.startTime) {
            return (
              time.getTime() <
              new Date(this.searchForm.startTime).getTime() -
                1 * 24 * 60 * 60 * 1000
            ); //可以选择同一天
          }
        },
      },
      searchForm: {
        orderSn: null,
        payChannelList: null,
        startTime: null,
        endTime: null,
      },

      tableLoading: false,
      tableData: [],
      pageNum: 1,
      pageSize: 15,
      total: 0,
    };
  },

  computed: {
    ...mapGetters(["permissions"]),
  },
  created() {
    this.getOrderList();
  },
  methods: {
    async paydownloadClick() {
      const res = await this.$api.payUploadFile({
        condition: {
          ...this.searchForm,
        },
      });
      console.log(res);
      this.filename = decodeURIComponent(res.headers["filename"]);
      let blob = new Blob([res.data],{type:'application/vnd.ms-excel;charset=UTF-8'});
      var link = document.createElement("a");
      var href = window.URL.createObjectURL(blob);
      link.href = href;
      link.download = '支付流水';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      window.URL.revokeObjectURL(href);
      this.$message.success("请稍等,正在下载");
      // this.content = res.data;
      // this.filename = decodeURIComponent(res.headers["filename"]);
      // const blob = new Blob([this.content]);
      // if (window.navigator.msSaveOrOpenBlob) {
      //   // 兼容IE10
      //   navigator.msSaveBlob(blob, this.filename);
      // } else {
      //   let aTag = document.createElement("a");
      //   aTag.download = this.filename;
      //   aTag.href = URL.createObjectURL(blob);
      //   aTag.click();
      //   URL.revokeObjectURL(aTag.href);
      // }
      // this.$message.success("请稍等,正在下载");
    },
    searchClick() {
      this.pageNum = 1;
      this.getOrderList();
    },
    resetClick() {
      this.$refs.searchFormRef.resetFields();
      this.searchForm.startTime=''
      this.searchForm.endTime=''
      this.pageNum = 1;
      this.getOrderList();
    },

    /**
     * 获取用户列表
     * @param {*}
     */
    async getOrderList() {
      this.tableLoading = true;
      await this.$api
        .payList({
          size: this.pageSize,
          page: this.pageNum,
          condition: {
            ...this.searchForm,
          },
        })
        .then((res) => {
          this.total = res.data.result.total;
          this.tableData = res.data.result.records;
          this.tableLoading = false;
        });
    },

    /**
     * 切换页面大小
     * @param {*} val
     */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getOrderList();
    },

    /**
     * 页码切换
     * @param {*} val
     */
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.getOrderList();
    },
  },
};
</script>

<style lang="less" scoped>
.accountList {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  .searchBox {
    .el-row {
      display: flex;
      flex-wrap: wrap;
      .el-form-item {
        margin-bottom: 0;
      }
      .item {
        display: flex;
        align-items: center;
        margin-right: 20px;
        .nameSpan {
          padding-right: 10px;
          box-sizing: border-box;
          white-space: nowrap;
        }
        .el-input {
          width: 162px;
        }
      }
    }
  }
  .topBtn {
    margin: 10px 0;
    text-align: right;
  }
}
/deep/.el-button--success.is-plain {
  color: #fff;
  border-color: #409eff;
  background-color: #409eff;
}
/deep/.el-form-item {
  margin-bottom: 12px;
}
</style>
